module scene {

    export class ReliveScene extends eui.Component {
        private cutTime: number = 9
        private reliveTimeText: eui.Label
        private rejectBtn: eui.Label
        private reliveBtn: eui.Image
        private timer
        private score: number
        private level: number
        private reliveNum: number
        private reliveText
        constructor(score, level, reliveNum) {
            super();
            this.level = level
            this.reliveNum = reliveNum
            this.score = score
            this.addEventListener(eui.UIEvent.COMPLETE, this.onComplete, this);
            this.skinName = "relivePageSkin";
        }




        public onComplete() {
            manager.StatisticsManager.sceneCount(1007)
            this.timer = setInterval(() => {
                this.cutTime --
                this.reliveTimeText.text = `${this.cutTime}`
                if(this.cutTime == 0){
                    this.onReject()
                }
            }, 1000)

            this.rejectBtn.visible = false
            setTimeout(() => {
                this.rejectBtn.visible = true
            }, 500)

            this.reliveText.text = `复活:${this.reliveNum} / 3`

            this.rejectBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onReject, this)

            this.reliveText.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onRelive, this)
        }

        private onReject() {
            manager.StatisticsManager.click(156)
            console.log('onReject')
            clearInterval(this.timer)
            let event: events.GameFinishEvent = new events.GameFinishEvent(this.score, this.level);
            this.dispatchEvent(event)
        }


        private onRelive() {    
            console.log('onRelive')   
            manager.StatisticsManager.click(155)

            clearInterval(this.timer)
     
            let adManager = manager.GameManager.getInstance().getAdManager();
            let revivalMethod = Number(adManager.ad_percent) - Math.random() * 100
            if(revivalMethod >= 0){
                adManager.showVideoAd();
                adManager.addEventListener(manager.AdManager.VIDEO_AD_CLOSE,
                    this.onVideoAdClose, this);
            }else{
                manager.ShareManager.getInstance().addEventListener(manager.ShareManager.SHARE_STATUS,
                    this.onShareStatus, this);
                manager.GameManager.getInstance().commonShare();
            }

            // this.onShareSucc()
            
        }

        private onShareStatus(eve: egret.Event): void {
            manager.ShareManager.getInstance().removeEventListener(manager.ShareManager.SHARE_STATUS,
                this.onShareStatus, this);
            let succ: number = eve.data;
            if (succ > 0) {
                this.onShareSucc();
            }
        }

        private onVideoAdClose(eve: egret.Event): void {
            egret.log("视频广告播放事件:", eve.data);
            if (eve.data != null && eve.data.isEnded) {
                manager.GameManager.getInstance().adManager.removeEventListener(manager.AdManager.VIDEO_AD_CLOSE,
                    this.onVideoAdClose, this);
                this.onShareSucc();
            }
        }

        private onShareSucc(): void {
            let event: events.ReliveEvent = new events.ReliveEvent();
            this.dispatchEvent(event)
        }
    }
}
